/**************************************************
|	File:		MainMenuState.cpp
|	Author:		Benjamin Budian
|	Course:		SGD
|	Purpose:	CMainMenuState class handles the 
|				main menu screen.
**************************************************/

#include "MainMenuState.h"

#include "../SGD Wrappers/CSGD_Direct3D.h"
#include "../SGD Wrappers/CSGD_DirectInput.h"

#include "Game.h"
#include "GameplayState.h"
#include "OptionsMenuState.h"
#include "HowToPlayState.h"
#include "CreditsState.h"




/*************************************************/
// GetInstance
//	- store the SINGLETON instance in global memory, 
//	  rather than on the heap
/*static*/ CMainMenuState* CMainMenuState::GetInstance( void )
{
	static CMainMenuState s_Instance;
	return &s_Instance;
}


/*************************************************/
// Constructor
//	- set ALL data members to 'clean' values
CMainMenuState::CMainMenuState(void) { m_nCursor = 0;}

/*************************************************/
// Destructor
CMainMenuState::~CMainMenuState(void){}

/*************************************************/
// Enter
//	- load resources
//	- reset data (optional)
/*virtual*/ void CMainMenuState::Enter( void )
{
	// Don't reset the cursor, so it will keep its previous position
	//m_nCursor = 0;
}


/*************************************************/
// Exit
//	- unload resources
/*virtual*/ void CMainMenuState::Exit( void ){}


/*************************************************/
// Input
//	- handle user input
/*virtual*/ bool CMainMenuState::Input( void )
{
	CSGD_DirectInput* pDI = CSGD_DirectInput::GetInstance();

	// Navigate the menu
	if( pDI->KeyPressed( DIK_DOWN ) == true )
	{
		++m_nCursor;

		// Stop at the bottom
		if( m_nCursor > EXIT )		
			m_nCursor = PLAY;
	}
	else if( pDI->KeyPressed( DIK_UP ) == true )
	{
		--m_nCursor;

		// Stop at the top
		if( m_nCursor < PLAY )		// PLAY
			m_nCursor = EXIT;
	}

	// Handle the Enter key
	if( pDI->KeyPressed( DIK_RETURN ) == true )
	{
		// What selection?
		switch( m_nCursor )
		{
		case PLAY:			 
			{
				// Change the state to CGameplayState
				CGame::GetInstance()->ChangeState( CGameplayState::GetInstance() );
				return true;	 
			}
			break;
		case OPTIONS:			
			{
				CGame::GetInstance()->ChangeState( COptionsMenuState::GetInstance() );
				return true;
			}
			break;
		case HOW_TO_PLAY:			 
			{
				CGame::GetInstance()->ChangeState( CHowToPlayState::GetInstance() );
				return true;
			}
			break;
		case CREDITS:			 
			{
				CGame::GetInstance()->ChangeState( CCreditsState::GetInstance() );
				return true;
			}
			break;
		case EXIT:			 
			{
				return false;
			}
			break;
		}
	}
	
	// Escape to exit
	if( pDI->KeyPressed( DIK_ESCAPE ) == true )
	{
		m_nCursor = EXIT;
		return true;
	}
	// Keep running
	return true;
}

/*************************************************/
// Update
//	- update the state
/*virtual*/ void CMainMenuState::Update( float fElapsedTime ){}

/*************************************************/
// Render
//	- render the main menu
/*virtual*/ void CMainMenuState::Render( void )
{
	CBitmapFont* pFont = CGame::GetInstance()->GetFont();

	// Render the title
	int nCenterOffset =  CGame::GetInstance()->GetScreenWidth()/2 - (strlen("ESCAPE")*64)/2;
	pFont->Draw( _T("ESCAPE"), 
		nCenterOffset, 
		100, 2.0f, D3DCOLOR_ARGB(255, 255, 255, 255) );

	// Render the menu
	nCenterOffset =  CGame::GetInstance()->GetScreenWidth()/2 - (strlen("play")*32)/2;
	pFont->Draw( _T("Play"),		nCenterOffset, 300, 1.0f, D3DCOLOR_ARGB(255, 255, 255, 255) );
	//pFont->Draw( _T("Load Game"),		nCenterOffset, 350, 1.0f, D3DCOLOR_ARGB(255, 255, 255, 255) );
	//nCenterOffset =  CGame::GetInstance()->GetScreenWidth()/2 - (strlen("options")*32)/2;
	nCenterOffset =  CGame::GetInstance()->GetScreenWidth()/2 - (strlen("options")*32)/2;
	pFont->Draw( _T("Options"),		nCenterOffset, 350, 1.0f, D3DCOLOR_ARGB(255, 255, 255, 255) );
	nCenterOffset =  CGame::GetInstance()->GetScreenWidth()/2 - (strlen("how to play")*32)/2;
	pFont->Draw( _T("How To Play"),	nCenterOffset, 400, 1.0f, D3DCOLOR_ARGB(255, 255, 255, 255) );
	nCenterOffset =  CGame::GetInstance()->GetScreenWidth()/2 - (strlen("credits")*32)/2;
	pFont->Draw( _T("Credits"),		nCenterOffset, 450, 1.0f, D3DCOLOR_ARGB(255, 255, 255, 255) );
	nCenterOffset =  CGame::GetInstance()->GetScreenWidth()/2 - (strlen("exit")*32)/2;
	pFont->Draw( _T("Exit"),		nCenterOffset, 500, 1.0f, D3DCOLOR_ARGB(255, 255, 255, 255) );

	// Render the cursor (><)
	nCenterOffset =  CGame::GetInstance()->GetScreenWidth()/2 - (strlen(">            <")*32)/2;
	pFont->Draw( _T(">            <"), nCenterOffset, 300 + (m_nCursor * 50), 1.0f, D3DCOLOR_ARGB(255, 255, 255, 255) );
}
